package org.gushi.datastruct.a02;

import java.util.Arrays;

public class ArrayList {

    private int size;

    private int[] elements;

    private static final int DEFAULT_CAPACITY = 2;
    private static final int ELEMENT_NOT_FOUND = -1;


    public ArrayList(int capacity) {
        capacity = capacity > DEFAULT_CAPACITY ? capacity : DEFAULT_CAPACITY;
        this.elements = new int[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

    public int size() {
        return size;
    }

    public void add(int element) {
//        elements[size] = element;
//        size++;

//        elements[size++] = element;

        add(size, element);
    }

    public void add(int index, int element) {
        rangeCheckForAdd(index);

        ensureCapacity(size + 1);

        //先移动
        for (int i = size; i > index; i--) {
            elements[i] = elements[i - 1];
        }
        //在赋值
        elements[index] = element;

        size++;

    }

    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) return;

        int newCapacity = oldCapacity + (oldCapacity >> 1);//后面一个*1.5 扩容为以前的1.5倍
        int[] newElements = new int[newCapacity];
        //此处存在线程安全问题的
        for (int i = 0 ;i<size;i++){
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("旧容量："+oldCapacity+"，新容量："+newCapacity);
    }

    public Object get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    //根据索引更新
    public Object set(int index, int element) {
        rangeCheck(index);
        int old = elements[index];
        elements[index] = element;
        return old;
    }

    public Object remove(int element) {
        int rs = indexOf(element);
        if (ELEMENT_NOT_FOUND == rs) return null;

        for (int i = rs; i < size; i++) {
            elements[i] = elements[i + 1];
        }
        size--;

        return null;
    }

    public boolean isEmpty() {
        return size == 0;
    }


    public boolean isContains(int element) {
        return ELEMENT_NOT_FOUND != indexOf(element);
    }


    public int indexOf(int element) {
        for (int i = 0; i < size; i++) {
            if (elements[i] == element) {
                return i;
            }
        }
        return ELEMENT_NOT_FOUND;
    }


    public void clear() {
        size = 0;
    }

    @Override
    public String toString() {
        return "ArrayList{" +
                "size=" + size +
                ", elements=" + Arrays.toString(elements) +
                '}';
    }


    private void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size);
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }


}
